//=============================================================================
//
//   Copyright (C) 2020 All rights reserved.
//
//   Filename:		fem.rs
//
//   Author: Wang Zhecheng - wangzhecheng@yeah.net
//
//   Date: 2020-10-19 00:50
//
//   Last Editors: Wang Zhecheng - wangzhecheng@yeah.net
//
//   Last modified:	2020-11-06 18:27
//
//   Description:
//
//=============================================================================
use crate::component::{element::ElementLike, model::Model, node::Node};
use crate::dimension::DimType;
use crate::geometry::{
    mesh::Mesh,
    point::{Point, PointLike},
    shape::ShapeLike,
};
use crate::method::NumericalMethodLike;
use crate::Real;
use crate::util::{indexable::Indexable, HasName};
use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::{Rc, Weak};

#[derive(Debug)]
pub struct Fem<DT: DimType> {
    _marker: PhantomData<DT>,
}
default_dt_marker!(Fem<DT: DimType>);

impl<DT: DimType> NumericalMethodLike<DT> for Fem<DT> {
    fn build_mesh(&self, data: serde_json::Value, model: &mut Model<DT>) {
        log::trace!("Building nodes from source file...");
        // 1. build nodes, iterator all nodes record from xml source file
        for node_data in data["nodes"].as_array().expect("Failed to get nodes array") {
            let new_pt: Point<DT::Dim> =
                serde_json::from_value(node_data.clone()).expect("Failed to deserialize point");
            let weak_pt = model.append_point(new_pt);
            let mut node = Node::<DT>::default();
            node.append_point(weak_pt);
            let weak_node = model.append_shape(Rc::new(RefCell::new(node)));
        }
    }
}
